//Gemaakt door Y. Laenen C1011950
//Datum: 25 11 2013

package views;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import javax.swing.JButton;

import controllers.createControllers.ToevoegenBetalingsGegController;
import controllers.createControllers.ToevoegenPersGegBookingController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.createModels.ToevoegenBookingPersGegModel;

import java.awt.Color;

public class BookingPersoonsGegToevoegen extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txtVoornaam;
	private JTextField txtAchternaam;
	private JTextField txtStraat;
	private JTextField txtBus;
	private JTextField txtNummer;
	private JTextField txtPostcode;
	private JTextField txtGemeente;
	private JTextField txtStaatProv;
	private JTextField txtIdNr;
	private JTextField txtDate;

	private ToevoegenBookingPersGegModel modelIn;

	@SuppressWarnings("rawtypes")
	public BookingPersoonsGegToevoegen(final ToevoegenBookingPersGegModel model) {

		getContentPane().setBackground(Color.LIGHT_GRAY);

		this.modelIn = model;

		setTitle("Persoonsgegevens invullen");

		JLabel lblVluchtInfo = new JLabel("<html>" + "Vluchtgegevens:" + "<br>"
				+ "--------------------------------------------" + "<br>"
				+ model.getVlucht().getVluchtNr() + "<br>"
				+ model.getVlucht().getVertrekPlaats() + " - "
				+ model.getVlucht().getAankomstPlaats() + "<br>"
				+ model.getVlucht().getVertrekTijdStip() + " - "
				+ model.getVlucht().getAankomstTijdStip() + "<br>"
				+ "--------------------------------------------" + "</html>");

		JLabel lblPersoonsgegevens = new JLabel("Persoonsgegevens");
		lblPersoonsgegevens.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC,
				14));

		txtVoornaam = new JTextField();
		txtVoornaam.setColumns(10);

		JLabel lblVoornaam = new JLabel("Voornaam");

		txtAchternaam = new JTextField();
		txtAchternaam.setColumns(10);

		JLabel lblAchternaam = new JLabel("Achternaam");

		txtStraat = new JTextField();
		txtStraat.setColumns(10);

		txtBus = new JTextField();
		txtBus.setColumns(10);

		JLabel lblAdresLijn1 = new JLabel("Straat + nummer + bus");

		txtNummer = new JTextField();
		txtNummer.setColumns(10);

		JLabel lblPostcodeGemeente = new JLabel("Postcode + gemeente");

		txtPostcode = new JTextField();
		txtPostcode.setColumns(10);

		txtGemeente = new JTextField();
		txtGemeente.setColumns(10);

		JLabel lblStaatProvincie = new JLabel("Staat / Provincie");

		txtStaatProv = new JTextField();
		txtStaatProv.setColumns(10);

		JLabel lblNationaliteit = new JLabel("Nationaliteit");

		final JComboBox<String> cboxNationaliteit = new JComboBox<String>();

		for (String nation : this.modelIn.getNationaliteiten()) {
			cboxNationaliteit.addItem(nation);
		}

		JLabel txtIdNummer = new JLabel("IdentiteitskaartNr.");

		txtIdNr = new JTextField();
		txtIdNr.setColumns(10);

		// DateTimePicker
		JDatePanelImpl datePanel = new JDatePanelImpl(new UtilDateModel(null));
		final JDatePickerImpl picker = new JDatePickerImpl(datePanel);

		JLabel lblGeboorteDatum = new JLabel("Geboortedatum");

		final JButton btnSubmit = new JButton("Volgende");

		// kijken of het scherm als updatevenster gebruikt wordt en nodige zaken
		// invullen/wijzigen
		if (modelIn.getSchermType() == "update") {
			btnSubmit.setText("Update");
			this.setTitle("Update Passagier");
			lblVluchtInfo
					.setText("U staat op het punt een bestaande passagier te wijzigen.");

			txtAchternaam.setText(modelIn.getNaam());
			txtVoornaam.setText(modelIn.getVoornaam());
		}

		// actionlistener
		btnSubmit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnSubmit) {
					String datum = "";
					datum += picker.getModel().getDay() + " "
						   + picker.getModel().getMonth() + " "
						   + picker.getModel().getYear();
					modelIn.setGeboortedatum(datum);

					// errorHandling
					Boolean noErrors = true;

					if (ValidatieService.isPresent(txtStraat, "Straatnaam")) {
						modelIn.setStraat(txtStraat.getText());
					} else {
						noErrors = false;
					}
					if (ValidatieService.isPositiveInteger(txtNummer,
							"Straatnummer")) {
						modelIn.setStraatNummer(Integer.parseInt(txtNummer
								.getText()));
					} else {
						noErrors = false;
					}
					modelIn.setBus(txtBus.getText());
					if (ValidatieService.isPresent(txtGemeente, "Gemeente")) {
						modelIn.setGemeente(txtGemeente.getText());
					} else {
						noErrors = false;
					}
					if (ValidatieService.isPresent(txtPostcode, "Postcode")) {
						modelIn.setPostcode(txtPostcode.getText());
					} else {
						noErrors = false;
					}
					if (ValidatieService.isPresent(txtStaatProv,
							"Staat/Provincie")) {
						modelIn.setProvincie(txtStaatProv.getText());
					} else {
						noErrors = false;
					}
					modelIn.setNationaliteit(cboxNationaliteit
							.getSelectedItem().toString());
					if (ValidatieService.isPresent(txtAchternaam, "Naam")) {
						modelIn.setNaam(txtAchternaam.getText());
					} else {
						noErrors = false;
					}
					if (ValidatieService.isPresent(txtVoornaam, "Voornaam")) {
						modelIn.setVoornaam(txtVoornaam.getText());
					} else {
						noErrors = false;
					}
					if (ValidatieService.isPresent(txtIdNr,
							"Identiteitskaartnummer")) {
						modelIn.setIdentiteitskaartNr(txtIdNr.getText());
					} else {
						noErrors = false;
					}

					if (noErrors) {
						ToevoegenPersGegBookingController.handleView(modelIn);
						ToevoegenBetalingsGegController.showview(
								model.getPassagier(), model.getVlucht(),
								model.getTypeVlucht());
					}
				}
			}
		});

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(42)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnSubmit, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblVluchtInfo, GroupLayout.PREFERRED_SIZE, 380, GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(lblPersoonsgegevens, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
										.addContainerGap())
									.addGroup(groupLayout.createSequentialGroup()
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addComponent(lblVoornaam)
											.addComponent(lblGeboorteDatum, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE))
										.addGap(81)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addGroup(groupLayout.createSequentialGroup()
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(txtStraat, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
												.addGap(18)
												.addComponent(txtNummer, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
												.addGap(18)
												.addComponent(txtBus, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE))
											.addComponent(txtVoornaam, GroupLayout.PREFERRED_SIZE, 298, GroupLayout.PREFERRED_SIZE)
											.addComponent(txtAchternaam, GroupLayout.PREFERRED_SIZE, 298, GroupLayout.PREFERRED_SIZE)
											.addGroup(groupLayout.createSequentialGroup()
												.addPreferredGap(ComponentPlacement.RELATED)
												.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
													.addComponent(txtStaatProv, GroupLayout.PREFERRED_SIZE, 298, GroupLayout.PREFERRED_SIZE)
													.addGroup(groupLayout.createSequentialGroup()
														.addComponent(txtPostcode, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
														.addGap(18)
														.addComponent(txtGemeente, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE)
														.addGap(0, 0, Short.MAX_VALUE))
													.addComponent(cboxNationaliteit, GroupLayout.PREFERRED_SIZE, 298, GroupLayout.PREFERRED_SIZE)
													.addComponent(txtIdNr, GroupLayout.PREFERRED_SIZE, 298, GroupLayout.PREFERRED_SIZE)
													.addComponent(picker, GroupLayout.PREFERRED_SIZE, 298, GroupLayout.PREFERRED_SIZE))))
										.addGap(581)))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(txtIdNummer, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblNationaliteit, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblStaatProvincie, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblPostcodeGemeente, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblAdresLijn1, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblAchternaam, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
									.addGap(907))))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(26)
					.addComponent(lblVluchtInfo, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
					.addGap(43)
					.addComponent(lblPersoonsgegevens)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(21)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblVoornaam)
								.addComponent(txtVoornaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addComponent(lblAchternaam)
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblAdresLijn1)
									.addGap(18)
									.addComponent(lblPostcodeGemeente))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(24)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(txtPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(txtGemeente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(52)
							.addComponent(txtAchternaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(12)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtStraat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtNummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtBus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(14)
							.addComponent(lblStaatProvincie))
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txtStaatProv, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(14)
							.addComponent(lblNationaliteit)
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtIdNummer)
								.addComponent(txtIdNr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(cboxNationaliteit, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(6)
							.addComponent(picker, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblGeboorteDatum)))
					.addGap(18)
					.addComponent(btnSubmit)
					.addContainerGap(35, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}

}
